package com.eduction.controller;

import com.eduction.bean.SessionUser;
import com.eduction.common.ReturnCode;
import com.eduction.common.ZipDownloadUtil;
import com.eduction.modules.system.entity.SchoolItemData;
import com.eduction.resource.BaseController;
import com.eduction.service.SchoolItemDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/testZip")
public class TestZipDownLoad extends BaseController {

    @Value("${doc.path}")
    private String docPath;
    @Autowired
    private SchoolItemDataService schoolItemDataService;

    @RequestMapping(value = "/download")
    @ResponseBody
    public void download(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "school_id", required = true) String school_id,
                         @RequestParam(value = "school_name", required = true) String school_name,@RequestParam(value = "item_id", required = true) String itemId) {
        try {
            String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
            if(StringUtils.isEmpty(user_type)){
                return;
            }
//            String schoolId = map.get("school_id");
//            String schoolId ="1495070177407001";
            if(school_name==null){
                school_name="ces";
            }
            List<SchoolItemData> list = schoolItemDataService.querySchoolItemDataBySchoolId(itemId,school_id);
            List<String> fs = new ArrayList<>();
            for (SchoolItemData sid : list) {
                String file = docPath + sid.getS_value();
                fs.add(file);
            }


            ZipDownloadUtil.downloadZip(fs, school_name, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
